package org.skipper.samples.spring.transaction.service;

import org.skipper.samples.spring.transaction.entity.UserInfo;
import org.skipper.samples.spring.transaction.enums.GenderEnum;
import org.skipper.samples.spring.transaction.mapper.UserInfoMapper;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * Created with IntelliJ IDEA.
 * Description: 事务传播特性实例
 *
 * @author shoujian
 * Date: 2022-02-12
 * Time: 2:20 PM
 */
@Component
public class PropagationService {

    @Resource
    private UserInfoMapper userInfoMapper;

    public UserInfo unTransactional() {

        try {
            UserInfo userInfo = new UserInfo();
            userInfo.setUserId(33L);
            userInfo.setNickname("昵称33");
            userInfo.setGender(GenderEnum.MALE);
            userInfoMapper.insert(userInfo);
            userInfo.setNickname("新昵称33");
            userInfoMapper.insert(userInfo);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

        return userInfoMapper.selectById(33L);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void requiredTransactional() {

        UserInfo userInfo = new UserInfo();
        userInfo.setUserId(33L);
        userInfo.setNickname("昵称33");
        userInfo.setGender(GenderEnum.MALE);
        userInfoMapper.insert(userInfo);
        userInfo.setNickname("新昵称33");
        userInfoMapper.insert(userInfo);
    }


    @Transactional(propagation = Propagation.SUPPORTS)
    public void supportsTransactional() {

        UserInfo userInfo = new UserInfo();
        userInfo.setUserId(33L);
        userInfo.setNickname("昵称33");
        userInfo.setGender(GenderEnum.MALE);
        userInfoMapper.insert(userInfo);
        userInfo.setNickname("新昵称33");
        userInfoMapper.insert(userInfo);
    }

    @Transactional(propagation = Propagation.MANDATORY)
    public void mandatoryTransactional() {

        UserInfo userInfo = new UserInfo();
        userInfo.setUserId(33L);
        userInfo.setNickname("昵称33");
        userInfo.setGender(GenderEnum.MALE);
        userInfoMapper.insert(userInfo);
        userInfo.setNickname("新昵称33");
        userInfoMapper.insert(userInfo);
    }

    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void requiresNewTransactional() {

        UserInfo userInfo = new UserInfo();
        userInfo.setUserId(33L);
        userInfo.setNickname("昵称33");
        userInfo.setGender(GenderEnum.MALE);
        userInfoMapper.insert(userInfo);
        userInfo.setNickname("新昵称33");
        userInfoMapper.insert(userInfo);
    }

    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public void notSupportedTransactional() {

        UserInfo userInfo = new UserInfo();
        userInfo.setUserId(33L);
        userInfo.setNickname("昵称33");
        userInfo.setGender(GenderEnum.MALE);
        userInfoMapper.insert(userInfo);
        userInfo.setNickname("新昵称33");
        userInfoMapper.insert(userInfo);
    }

    @Transactional(propagation = Propagation.NEVER)
    public void neverTransactional() {

        UserInfo userInfo = new UserInfo();
        userInfo.setUserId(33L);
        userInfo.setNickname("昵称33");
        userInfo.setGender(GenderEnum.MALE);
        userInfoMapper.insert(userInfo);
        userInfo.setNickname("新昵称33");
        userInfoMapper.insert(userInfo);
    }

}
